package com.hhd.flow.controller;


import com.hhd.flow.vo.Result;
import com.hhd.flow.vo.StartProcessVo;
import com.hhd.flow.engine.FlowEngine;
import com.hhd.flow.engine.cmd.CommandResult;
import com.hhd.flow.engine.cmd.impl.StartProcessCommand;
import com.hhd.flow.engine.vo.FlowUser;
import com.hhd.flow.entity.FlowProcessInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/flow")
public class StartProcessController {
    @Autowired
    FlowEngine flowEngine;

    @Transactional
    @PostMapping("/startProcess")
    public Result startProcess(@RequestBody StartProcessVo startProcessVo) {
        FlowUser flowUser = new FlowUser();
        flowUser.setId("tangzy");
        flowUser.setUsername("唐泽宇");
        StartProcessCommand startProcessCommand = flowEngine.newStartProcessCommandBuilder()
                .formTypeId(startProcessVo.getFormTypeId())
                .formId(startProcessVo.getFormId())
                .userManualSelect(startProcessVo.getUserManualSelect())
                .formModel(startProcessVo.getFormModel())
                .build();
        CommandResult<FlowProcessInstance> result = flowEngine.execute(startProcessCommand);
        return Result.ok(result.getResult());
    }
}
